using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using System;
using System.Collections.Specialized;

namespace Microsoft.SqlServer.Management.SqlManagerUI.Scripting
{
using Server = Microsoft.SqlServer.Management.Smo.Server;
	internal class ScriptUDDTCommand : ScriptObjectCommand
	{
		private const string AddUddt = "EXEC dbo.sp_addtype @typename=N'{0}', @phystype='{1}'";

		private const string AddUddtNullOption = ", @nulltype='{0}'";

		private const string AddUddtOwnerOption80 = ", @owner=N'{0}'";

		private const string IfUddtNotExists = "IF NOT EXISTS (SELECT * FROM dbo.systypes WHERE name = N'{0}')\r\nBEGIN\r\n{1}\r\nEND\r\n";

		private TableChildObjects childObjects;

		protected bool ScriptBindings
		{
			get
			{
				return (this.childObjects & TableChildObjects.Bindings) > TableChildObjects.None;
			}
		}

		public ScriptUDDTCommand(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptAction, TableChildObjects childObjects) : base(server, scriptTarget, scriptAction)
		{
			this.childObjects = childObjects;
		}

		public override void Script(Urn objectToScript, ScriptingOptions scriptOptions)
		{
			base.Script(objectToScript, scriptOptions);
			this.ScriptRuleBindings(objectToScript, scriptOptions);
		}

		protected override void ScriptCreate(SqlSmoObject smoObject, ScriptingOptions options, ObjectScriptedHandler scriptTargetHandler)
		{
			base.ScriptCreate(smoObject, options, scriptTargetHandler);
		}

		private void ScriptRuleBindings(Urn objectToScript, ScriptingOptions scriptOptions)
		{
			if (!this.ScriptBindings)
			{
				return;
			}
			scriptOptions.Bindings = true;
			UserDefinedDataType userDefinedDataType = (UserDefinedDataType)this.GetSqlSmoObject(objectToScript);
			StringCollection stringCollection = new StringCollection();
			ReflectionUtilities.InvokeMethod(userDefinedDataType, "AddBindings", new object[]
			{
				stringCollection,
				scriptOptions
			});
			if (stringCollection.Count > 0)
			{
				base.ScriptTarget(new Microsoft.SqlServer.Management.SqlManagerUI.ObjectInfo(userDefinedDataType.Name, userDefinedDataType.Urn, userDefinedDataType.Schema), stringCollection, ScriptActions.Bindings);
			}
		}
	}
}
